package com.sinoyd.itszyw.Activity;

import android.os.Bundle;
import android.support.v4.view.ViewPager;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.Toast;

import com.sinoyd.itszyw.Adapter.MyFragmentManagerPageAdatper;
import com.sinoyd.itszyw.R;

import org.xutils.view.annotation.ContentView;
import org.xutils.view.annotation.ViewInject;

@ContentView(R.layout.activity_main)
public class MainActivity extends BaseActivity implements RadioGroup.OnCheckedChangeListener, ViewPager.OnPageChangeListener {

    private MyFragmentManagerPageAdatper mPageAdatper;

    @ViewInject(R.id.v_pager)
    private ViewPager mViewPager;
    @ViewInject(R.id.rb_channel)
    private RadioButton rb_channel;
    @ViewInject(R.id.rb_message)
    private RadioButton rb_message;
    @ViewInject(R.id.rb_more)
    private RadioButton rb_more;
    @ViewInject(R.id.rg_tab_bar)
    private RadioGroup rg_tab_bar;



    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        mPageAdatper = new MyFragmentManagerPageAdatper(getSupportFragmentManager());
        mViewPager.setAdapter(mPageAdatper);
        mViewPager.setCurrentItem(0);
        mViewPager.addOnPageChangeListener(this);
        rg_tab_bar.setOnCheckedChangeListener(this);
        rb_channel.setChecked(true);


    }


    private long exitTime = 0;

    @Override
    public void onBackPressed() {
        //super.onBackPressed();
        if ((System.currentTimeMillis() - exitTime) > 2000) {
            Toast.makeText(getApplicationContext(), "再按一次退出程序",
                    Toast.LENGTH_SHORT).show();
            exitTime = System.currentTimeMillis();
        } else {
            finish();
            System.exit(0);
        }

    }

    @Override
    public void onCheckedChanged(RadioGroup radioGroup, int i) {
        switch (i) {
            case R.id.rb_channel:
                mViewPager.setCurrentItem(0);
                break;
            case R.id.rb_message:
                mViewPager.setCurrentItem(1);
                break;
            case R.id.rb_more:
                mViewPager.setCurrentItem(2);
                break;
        }

    }

    @Override
    public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

    }

    @Override
    public void onPageSelected(int position) {

    }

    @Override
    public void onPageScrollStateChanged(int state) {

        if (state == 2) {
            switch (mViewPager.getCurrentItem()) {
                case 0:
                    rb_channel.setChecked(true);
                    break;
                case 1:
                    rb_message.setChecked(true);
                    break;
                case 2:
                    rb_more.setChecked(true);
                    break;
            }
        }

    }

}
